package kr.ac.kaist.simplefileexplorer;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class AddRecipientAdapter extends BaseAdapter {
	private List<ContactInfoItem> itemList;
	private Context context;
	public Hashtable<Integer, View> hashConvertView = new Hashtable<Integer, View>();
	
	public AddRecipientAdapter(List<ContactInfoItem> itemList, Context context) {
		this.itemList = itemList;
		this.context = context;
	}	
	
	public long getItemId(int position) {
		return itemList.get(position).getId();
	}	
	
	public int getCount() {
		return itemList.size();
	}

	public ContactInfoItem getItem(int position) {
		return itemList.get(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ContactInfoItem item = itemList.get(position);

		ViewHolder holder;
		if (hashConvertView.containsKey(position) == false) {
			convertView = LayoutInflater.from(context).inflate(R.layout.contact_item, parent, false);
			holder = new ViewHolder();

			holder.tvNumber = (TextView) convertView.findViewById(R.id.contact_number);
			holder.tvName = (TextView) convertView.findViewById(R.id.contact_name);
			holder.chk = (CheckBox) convertView.findViewById(R.id.contact_selected_checkbx);
			holder.chk.setId(position);
			holder.chk.setOnClickListener(listener);

			convertView.setTag(holder);
			hashConvertView.put(position, convertView);
		} else {
			convertView = (View) hashConvertView.get(position);
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvNumber.setText(item.getPhoneNumber());
		holder.tvName.setText(item.getName());

		return convertView;
	}
	
	
	private final OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			if (v instanceof CheckBox) {
				CheckBox chk = (CheckBox) v;

				// switch (chk.getId()) {
				// }
				notifyDataSetChanged();
				// notifyDataSetInvalidated();
			}
		}
	};	
	
	
	public Long[] getCheckItemIds() {
		if (hashConvertView == null || hashConvertView.size() == 0)
			return null;

        Integer key;
        View view;
		List<Long> lstIds = new ArrayList<Long>();
		Enumeration<Integer> e = hashConvertView.keys();
        long index = 0;
        while(e.hasMoreElements()) {
            key = (Integer)e.nextElement();
            view = (View)hashConvertView.get(key);
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder.chk.isChecked()){
            	lstIds.add(index);
            }
            index++;
        }
        
        Long[] arrLong = (Long[]) lstIds.toArray(new Long[0]);
        return arrLong;
	}
	
	public void clearChoices()
	{
		if (hashConvertView == null || hashConvertView.size() == 0)
			return;
		
        Integer key;
        View view;
		List<Long> lstIds = new ArrayList<Long>();
		Enumeration<Integer> e = hashConvertView.keys();
        long index = 0;
        while(e.hasMoreElements()) {
            key = (Integer)e.nextElement();
            view = (View)hashConvertView.get(key);
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder.chk.isChecked()){
            	holder.chk.setChecked(false);
            }
            index++;
        }		
	}
	
	static class ViewHolder {
		CheckBox chk;
		TextView tvNumber;
		TextView tvName;
	}
	
	
	
}